package com.hisysteTopet.hisystepet.controller;



import com.hisysteTopet.hisystepet.bean.Abouts;
import com.hisysteTopet.hisystepet.bean.Admin;
import com.hisysteTopet.hisystepet.bean.Msg;
import com.hisysteTopet.hisystepet.bean.User;
import com.hisysteTopet.hisystepet.service.AdminService;
import com.hisysteTopet.hisystepet.service.UserService;
import com.hisysteTopet.hisystepet.service.impl.AboutServiceImpl;
import com.hisysteTopet.hisystepet.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    // 设置token的值
    public static String  TOKEN;

    // 设置页面拦截
    public static int  viewInterceptors = 0;

    @Autowired
    private Msg msg;

    @Autowired
     User user;

    @Autowired
    private UserService userService;

    @Autowired
     Admin admin;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AboutServiceImpl aboutService;

    @Autowired
    private Abouts about;

    @ResponseBody
    @GetMapping("/message")
    public Msg message(@RequestParam("message")String message){
        about = aboutService.selectToMessage(message);
        return Msg.success().add("message",about);
    }

    @ResponseBody
    @RequestMapping(value = "/userToLogin",method = RequestMethod.POST)
    public Msg userToLogin(@RequestParam("username")String username,
                           @RequestParam("password")String password){
        user = userService.selectToMessage(username);
        admin = adminService.selectByUsername(username);
        if (user != null){
            if (password.equals(user.getPassword())){
                viewInterceptors = 1;
                Map<String, String> payload = new HashMap<>();
                payload.put("username", user.getUsername());
                TOKEN = JWTUtils.getToken(payload);
                msg = Msg.success().add("depts","1")
                        .add("token",TOKEN);
            }else {
                msg = Msg.fail();
            }
        }else if (admin != null){
            if (password.equals(admin.getPassword())){
                viewInterceptors = 2;
                Map<String, String> payload = new HashMap<>();
                payload.put("username", admin.getUsername());
                System.out.println("我是username"+admin.getUsername());
                TOKEN = JWTUtils.getToken(payload);
                msg = Msg.success().add("depts","2")
                        .add("token",TOKEN);
            }else {
                msg = Msg.fail();
            }

        }else {
            msg = Msg.fail();
        }



        return msg;
    }


}
